#' ---
#' author: "```matthew.pietryka@gmail.com```"
#' date: "2019-07-26"
#' output:
#'  html_document:
#'    warning: false
#'    toc: true
#'    code_folding: hide
#' ---


#' **PURPOSE:**
#' This script creates plots to display the relationship between party and text similarity (Figure 4A)"



# GET STARTED ---------------

#  load packages
library(tidyverse)


#  load data and plotting preferences
source("SC-Plots-Load-the-Data.R")
source("SC-Plots-Preferences.R")


# DOT PLOT ------------------------

plot_df <- df_new  %>%
  mutate(party_same_lab = recode(party_same,`1` = "Same\nparty",  `0` = "Mixed",
                                 `-1` = "Opposing\nparties")  %>%
      fct_reorder(party_same))  %>%
  group_by(party_same_lab)  %>%
  summarise(mean = mean(ratio),
            sd = sd(ratio),
            n = n(),
            se = sd/sqrt(n),
            cv =  qt(0.975, n),
            lb = mean - cv * se,
            ub = mean + cv * se
  )

dot_plot <- ggplot(plot_df, aes(
  x = party_same_lab,
  y = mean,
  label = party_same_lab)
) +
  geom_linerange(aes(ymin = lb, ymax = ub), size = 1.3) +
  geom_point(size = 5) +
  geom_text(aes(y = lb), size = 4.5, vjust = 1, fontface = "bold") +
  expand_limits(y = 0) +
  theme_sc(axis_title_size = 12, base_size = 12) +
  ggtitle(NULL) +
  xlab("Partisan congruence between focal and source legislatures") +
  ylab("Average proportion borrowed") +
  theme(
    legend.position = "none",
    panel.grid.major.x = element_blank(),
    axis.text.x = element_blank())

graphics.off()
windows(5, 3.5)
dot_plot




# SAVE ---------------

graphics.off()
windows(5, 4)
dot_plot


ggsave("Plots/party_means.png")



